/* eslint-disable no-extend-native */
// 中国标准时间转换 /yyyy|MM|dd|HH|mm|ss
var formatOne = function(time, format) {
  var t = new Date(time)
  var tf = function(i) { return (i < 10 ? '0' : '') + i }
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear())
      case 'MM':
        return tf(t.getMonth() + 1)
      case 'mm':
        return tf(t.getMinutes())
      case 'dd':
        return tf(t.getDate())
      case 'HH':
        return tf(t.getHours())
      case 'ss':
        return tf(t.getSeconds())
    }
  })
}
// 时间戳转换当前时间
/**
 * 传入参数格式
 *  (1514736000000, 'yyyy-MM-dd')
 *  (1514736000000, 'yyyy-MM-dd hh:mm:ss')
 * */
var dateFormat = function(millionsTime, pattern) {
  var d = new Date()
  d.setTime(millionsTime)
  var date = {
    'M+': d.getMonth() + 1,
    'd+': d.getDate(),
    'h+': d.getHours(),
    'm+': d.getMinutes(),
    's+': d.getSeconds(),
    'q+': Math.floor((d.getMonth() + 3) / 3),
    'S+': d.getMilliseconds()
  }
  if (/(y+)/i.test(pattern)) {
    pattern = pattern.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in date) {
    if (new RegExp('(' + k + ')').test(pattern)) {
      pattern = pattern.replace(RegExp.$1, RegExp.$1.length === 1
        ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
    }
  }
  return pattern
}
// 传入当前日期,获取周几

var dateDay = function(nowdate) {
  var days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  var nowday = days[ nowdate ]
  return nowday
}

Date.prototype.format = function() {
  var s = ''
  var mouth = (this.getMonth() + 1) >= 10 ? (this.getMonth() + 1) : ('0' + (this.getMonth() + 1))
  var day = this.getDate() >= 10 ? this.getDate() : ('0' + this.getDate())
  s += this.getFullYear() + '-' // 获取年份。
  s += mouth + '-' // 获取月份。
  s += day // 获取日。
  return (s) // 返回日期。
}

function getAll(begin, end) {
  var ab = begin.split('-')
  var ae = end.split('-')
  var db = new Date()
  db.setUTCFullYear(ab[0], ab[1] - 1, ab[2])
  var de = new Date()
  de.setUTCFullYear(ae[0], ae[1] - 1, ae[2])
  var unixDb = db.getTime()
  var unixDe = de.getTime()
  var all = []
  for (var k = unixDb; k <= unixDe;) {
    // console.log((new Date(parseInt(k))).format())
    k = k + 24 * 60 * 60 * 1000
    all.push((new Date(parseInt(k))).format())
  }
  return all
}

function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}
export { formatOne, dateFormat, dateDay, getAll, debounce }
